new Vue({
    el: '#app',
    data: {
        user: {
            name: '',
            idCard: '',
        },
        userCookie: {
            idCard: '',
            name: '',
            age: '',
            sex: '',
            email: '',
            phone: '',
            id: '',
            jcbNum: '',
            jcNum: '',
            address: '',
            grgl:'',
        },
        rules: {
            name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
            idCard: [{ required: true, message: '请输入身份证', trigger: 'blur' }]
        },
        vshow: {
            showButton: true,
            loading: false,
            userShow: false,
            showMsg: false,
        },
        pagesize: 5,
        jichubing:
            [{
                value: '0',
                label: '健康',
            }, {
                value: '1',
                label: '有一个基础病',
            }, {
                value: '2',
                label: '有两个基础病',
            }, {
                value: '3',
                label: '有三个基础病',
            }, {
                value: '4',
                label: '有四个基础病',
            }, {
                value: '5',
                label: '有五个及以上的基础病',
            }],
    },
    methods: {
        searchMsg: function (formName) {
            var _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    $.ajax({
                        url: 'common/getUserMsg',
                        data: {
                            name: _this.user.name,
                            idCard: _this.user.idCard
                        },
                        type: 'post',
                        dataType: 'Json',
                        success: function (data) {
                            if (data.code == 1) {
                                console.log(data);
                                _this.setCookie('idCard', _this.user.idCard, 1);
                                _this.setCookie('name', _this.user.name, 1);
                                _this.setCookie('grgl', data.grgl,1);
                                _this.userCookie.age = data.age;
                                _this.userCookie.sex = data.sex;
                                _this.userCookie.email = data.email;
                                _this.userCookie.phone = data.phone;
                                _this.userCookie.id = data.id;
                                _this.userCookie.jcbNum = data.jcbNum;
                                _this.userCookie.jcNum = data.jcNum;
                                _this.userCookie.address = data.address;
                                _this.vshow.userShow = true;
                                _this.userCookie.idCard = _this.getCookie('idCard');
                                _this.userCookie.name = _this.getCookie('name');
                                if (data.address == null) {
                                    $('#citys').citys();
                                } else {
                                    var strs = new Array();
                                    strs = data.address.split("|");
                                    if (strs.length == 2) {
                                        $('#citys').citys({
                                            type: 'name', province: strs[0],
                                            city: strs[1],
                                            area: strs[1]
                                        })
                                    } else {
                                        $('#citys').citys({
                                            type: 'name', province: strs[0],
                                            city: strs[1],
                                            area: strs[2]
                                        });
                                    }
                                }
                                _this.vshow.showButton = false;
                                _this.vshow.showMsg = true;
                            } else if (data.code == 2) {
                                _this.$message.error('身份证已存在，与姓名不符');
                            } else {
                                _this.$message.error('系统异常');
                            }
                        }, error: function (data) {
                            _this.$message.error('系统异常');
                        }
                    });


                    // _this.vshow.loading = true;
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        searchInit: function () {
            var _this = this;
            $.ajax({
                url: 'common/getUserMsg',
                data: {
                    name: _this.userCookie.name,
                    idCard: _this.userCookie.idCard
                },
                type: 'post',
                dataType: 'Json',
                success: function (data) {
                    if (data.code == 1) {
                        _this.userCookie.age = data.age;
                        _this.userCookie.sex = data.sex;
                        _this.userCookie.email = data.email;
                        _this.userCookie.phone = data.phone;
                        _this.userCookie.id = data.id;
                        _this.userCookie.jcbNum = data.jcbNum;
                        _this.userCookie.jcNum = data.jcNum;
                        _this.userCookie.address = data.address;
                        if (data.address == null) {
                            $('#citys').citys();
                        } else {
                            var strs = new Array();
                            strs = data.address.split("|");
                            if (strs.length == 2) {
                                $('#citys').citys({
                                    type: 'name', province: strs[0],
                                    city: strs[1],
                                    area: strs[1]
                                })
                            } else {
                                $('#citys').citys({
                                    type: 'name', province: strs[0],
                                    city: strs[1],
                                    area: strs[2]
                                });
                            }
                        }
                        _this.vshow.showMsg = true;
                    }
                }, error: function (data) {
                    _this.$message.error('系统异常');
                }
            });
        },
        //设置cookie
        setCookie: function (cname, cvalue, exdays = 0) {
            var d = new Date();
            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
            var expires = "expires=" + d.toUTCString();
            console.info(cname + "=" + cvalue + "; " + expires);
            document.cookie = cname + "=" + cvalue + "; " + expires;
            console.info(document.cookie);
        },
        //获取cookie
        getCookie: function (cname) {
            var name = cname + "=";
            var ca = document.cookie.split(';');
            console.log("获取cookie,现在循环")
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                console.log(c)
                while (c.charAt(0) == ' ') c = c.substring(1);
                if (c.indexOf(name) != -1) {
                    return c.substring(name.length, c.length);
                }
            }
            return "";
        },
        //清除cookie
        clearCookie: function (name) {
            this.setCookie(name, "", -1);
        },
        clearUser: function () {
            this.clearCookie('idCard');
            this.clearCookie('name');
            this.clearCookie('grgl');
            this.QINGKONGDUIXIANGZHI(this.userCookie);
            this.QINGKONGDUIXIANGZHI(this.user);
            this.vshow.userShow = false;
            this.vshow.showButton = true;
            this.vshow.showMsg = false;
        },
        pageInit: function () {
            var _this = this;
            _this.userCookie.idCard = _this.getCookie("idCard");
            _this.userCookie.name = _this.getCookie("name");
        },
        QINGKONGDUIXIANGZHI: function (obj) {
            for (var key in obj) {
                obj[key] = '';
            }
        },
        selectPage: function (key, keyPath) {
            $(window).attr('location', keyPath);
        },
        submitMsg: function () {
            var _this = this;
            var provinceoptions = $('#province option:selected');
            var cityoptions = $('#city option:selected');
            var areaoptions = $('#area option:selected');
            if (typeof (areaoptions.val()) == "undefined") {
                _this.userCookie.address = provinceoptions.val() + '|' + cityoptions.val();
            } else {
                _this.userCookie.address = provinceoptions.val() + '|' + cityoptions.val() + '|' + areaoptions.val();
            }
            var strs = new Array();
            strs = _this.userCookie.address.split("|");
            if (strs.length == 2) {
                if (strs[0] == '- 请选择 -') {
                    _this.$message.info('请补充居住城市');
                    return;
                } else if (strs[1] == '- 请选择 -') {
                    _this.$message.info('请补充居住城市');
                    return;
                }
            } else {
                if (strs[0] == '- 请选择 -') {
                    _this.$message.info('请补充居住城市');
                    return;
                } else if (strs[2] == '- 请选择 -') {
                    _this.$message.info('请补充居住城市');
                    return;
                }
            }
            $.ajax({
                url: 'common/updateUserMsg',
                data: {
                    id: _this.userCookie.id,
                    age: _this.userCookie.age,
                    sex: _this.userCookie.sex,
                    email: _this.userCookie.email,
                    phone: _this.userCookie.phone,
                    jcbNum: _this.userCookie.jcbNum,
                    jcNum: _this.userCookie.jcNum,
                    address: _this.userCookie.address
                },
                type: 'post',
                dataType: 'Json',
                success: function (data) {
                    if(data.code == 1){
                        _this.$message({
                            showClose: true,
                            message: '更新成功',
                            type: 'success'
                        });
                    } else{
                        _this.$message.error('系统异常');
                    }

                }, error: function (data) {
                    _this.$message.error('系统异常');
                }
            });
        },

    },
    computed: {

    },
    mounted() {
        var _this = this;
        _this.pageInit();
        if (_this.getCookie('idCard') !== '') {
            _this.vshow.userShow = true;
            _this.vshow.showButton = false;
            _this.searchInit();
        } else {
            _this.vshow.showMsg = false;
        }
    },
})